package com.woniuxy.portal.model.vo;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

/**
 * 购物车项
 */
@Data
@NoArgsConstructor
public class CartItemVo {
    private Long userId;      //用户id
    private Long bookId;      //图书id
    private String press;     //出版社
    private String name;      //书名
    private String image;     //图书图片
    private BigDecimal price; //单价
    private Integer quantity; //数量
    private BigDecimal sum;   //金额

    //带部分参数的构造器
    public CartItemVo(Long userId, Long bookId, String name, String image, BigDecimal price, Integer quantity) {
        this.userId = userId;
        this.bookId = bookId;
        this.name = name;
        this.image = image;
        this.price = price;
        this.quantity = quantity;
    }

    /**
     * 计算金额的方法
     * 通过价格和数量相乘得到总金额
     *
     * @return BigDecimal 返回价格与数量的乘积，如果价格或数量为空则返回0
     */
    public BigDecimal getSum() {
        // 如果价格或数量为空，返回0
        if (price == null || quantity == null) {
            return BigDecimal.ZERO;
        }
        // 使用BigDecimal进行精确计算，避免浮点数精度问题
        return price.multiply(new BigDecimal(quantity));
    }

}
